#ifndef _CHUNK_ALLOCATOR_H_
#define _CHUNK_ALLOCATOR_H_

#include "../Allocator.h"

/*
	class : ChunkAllocator
	desc : As the name implies this allocates memory in
		chunks. It doesn't require the user to have
		to free memory but you should do it anyway.
		It uses a stack format to allocate memory.
		You must supply an address and a size.
*/
class ChunkAllocator : public Allocator
{
	public :
	unsigned char* data;
	unsigned int size;
	unsigned int remaining;
	unsigned long int top;
	ChunkAllocator(unsigned char* d, unsigned int s)
	{
		data=d; 
		size = s;
		remaining = s;
		top = 0;
	}
	unsigned long int allocate(unsigned long int size)
	{
		unsigned int i = 0;//zero out the memory allocated first
		unsigned int t2 = top;
		for(i = 0; i < size; i++)
			data[top+i] = 0;
		//get new top.
		top = top + size;
		//set new remaining value
		remaining = remaining - size;
		
		return top;
	}
	unsigned long int save(void* data, unsigned long int size)
	{
		//check if data is in the allocator already return data
		if(data <= this->data && this->data+this->size > data+size)
			return data;
		else
		{
			unsigned int i = 0;
			unsigned int t2=top;
			for(i = 0; i < size; i++)
				this->data[top+i] = *((unsigned char*) (data+i));
			//set top
			remaining = remaining - size;
			top = top+size;
			return t2;
		}
	}
	void load(void* buffer, unsigned long int size, unsigned long int address)
	{
		unsigned long int i = 0;
		void* d = (void*) address;
		for(i = 0; i < size; i++)
		{
			*((unsigned char*) (buffer+i)) = 
					*((unsigned char*) (d+i));
		}
	}
	unsigned long int unallocate(unsigned long int addr)
	{
		//in this case do nothing no need to delete for this allocator
		return addr;
	}
	unsigned long int remainingSpace(){return remaining;}

	
};


#endif
